﻿/*-----------------------------------------------
// File: BuffTransform0.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class BuffTransform0 : MonoBehaviour
    {
        public BattleCharacter ownBC;
        public UIGrid grid;

        Dictionary<string, GameObject> icons = new Dictionary<string, GameObject>();

        void Start() {
            grid = GetComponent<UIGrid>();
        }
        public void Init(BattleCharacter bc) {
            this.ownBC = bc;
        }

        public void Show() {
            gameObject.SetActive(true);
            Update();
        }
        public void Hide() { gameObject.SetActive(false); }

        void Update() {
            transform.localScale = Vector3.one;
            transform.localEulerAngles = Vector3.zero;
            transform.position = GetPosition();
        }

        Vector3 GetPosition() {
            if (ownBC != null && BattleViewCtrl.GetInstance().battleCamera!= null && UICamera.currentCamera != null)
            {
                Vector3 pos = Vector3.zero;
                var trans = ownBC.GetBindTransform("buffa");
                if (trans != null)
                {
                    pos = trans.position;
                    var screenPos = BattleViewCtrl.GetInstance().battleCamera.WorldToViewportPoint(pos);
                    bool onScreen = screenPos.z > 0 && screenPos.x > 0 && screenPos.x < 1 && screenPos.y > 0 && screenPos.y < 1;
                    if (onScreen)
                    {

                        pos = Battle.View.BattleViewCtrl.GetInstance().battleCamera.WorldToScreenPoint(pos);
                        pos = UICamera.currentCamera.ScreenToWorldPoint(pos);
                        pos.z = 0f;
                        return pos;
                    }
                    else
                    {
                        return new Vector3(10000, 10000, 10000);
                    }
                }
            }
            return new Vector3(10000, 10000, 10000);
        }


        public void AddBuffIcon(string iconName)
        {
            if (icons.ContainsKey(iconName))
            {
                icons[iconName].GetComponent<BuffIcon0>().AddCount();
            }
            else {
                var buffIcon = Battle.View.BattleFactory.SpawnBuffIcon();
                buffIcon.GetComponent<BuffIcon0>().Init(iconName);
                buffIcon.transform.SetParent(transform);
                buffIcon.transform.localPosition = Vector3.zero;
                buffIcon.transform.localScale = Vector3.one;
                buffIcon.transform.localEulerAngles = Vector3.zero;
                icons.Add(iconName, buffIcon);
            }
            grid.repositionNow = true;
        }

        public void RemoveBuffIcon(string iconName)
        {
            if (icons.ContainsKey(iconName))
            {
                icons[iconName].GetComponent<BuffIcon0>().SubCount();
                if (icons[iconName].GetComponent<BuffIcon0>().GetCount() <= 0)
                {
                    Battle.View.BattleFactory.UnSpawn(icons[iconName]);
                    icons.Remove(iconName);
                }
            }
            grid.repositionNow = true;
        }



    }
}